package com.sx.pipeline.app.di

import com.alibaba.android.arouter.facade.Postcard
import com.alibaba.android.arouter.facade.callback.NavCallback
import com.alibaba.android.arouter.launcher.ARouter
import com.blankj.utilcode.util.ActivityUtils
import com.blankj.utilcode.util.SPUtils
import com.lxj.xpopup.XPopup
import com.lxj.xpopup.core.BasePopupView
import com.lxj.xpopup.interfaces.OnConfirmListener
import com.sx.mvvmlibs.app.AppActivityManager
import com.sx.mvvmlibs.base.ViewBindingBaseActivity
import com.sx.mvvmlibs.http.ResultEnum
import com.sx.pipeline.app.config.SPKey
import com.sx.pipeline.arouth.RouteManager
import com.sx.pipeline.arouth.RoutePath
import com.sx.pipeline.custom.dialog.CustomInterceptorPopup
import com.sx.pipeline.utils.CacheDataManager

import org.koin.core.qualifier.named
import org.koin.dsl.module
import java.util.concurrent.ConcurrentHashMap

/**
 * @Author: YY
 * @Date: 2022/5/20
 * @Description: TODO
 */

val appModule = module {
    // 全局单例的审核弹窗组 避免多次弹出
    single(named("pop_map")) { ConcurrentHashMap<Int, BasePopupView>(1) }
}

val factoryModule = module {
    // 接口请求被动弹出的pop
    factory(named("token_login")) {
        XPopup.Builder(AppActivityManager.currentActivity())
            .dismissOnBackPressed(false)
            .dismissOnTouchOutside(false)
            .asCustom(CustomInterceptorPopup(
                AppActivityManager.currentActivity() as ViewBindingBaseActivity<*, *>
            )
                .setTitleContent("提示", ResultEnum.UNAUTHORIZED.msg, "")
                .setCancelText("")
                .setConfirmText("退出")
                .setListener({
                    RouteManager.Login.intentToLogin(
                        AppActivityManager.currentActivity(), object : NavCallback() {
                            override fun onArrival(postcard: Postcard?) {
                                SPUtils.getInstance().remove(SPKey.SP_TOKEN)
                                CacheDataManager.clearAllCache(AppActivityManager.currentActivity())
                                ActivityUtils.finishAllActivities()
                            }
                        })
                }, null)
            ).show()
    }

    factory(named("not_approved")) {
        XPopup.Builder(AppActivityManager.currentActivity())
            .asCustom(
                CustomInterceptorPopup(AppActivityManager.currentActivity() as ViewBindingBaseActivity<*, *>)
                    .setTitleContent("提示", ResultEnum.NOT_APPROVED.msg, "")
                    .setCancelText("")
                    .setConfirmText("知道了")
            )
            .show()
    }

    factory(named("not_payword")) {
        XPopup.Builder(AppActivityManager.currentActivity())
            .asCustom(CustomInterceptorPopup(AppActivityManager.currentActivity() as ViewBindingBaseActivity<*, *>)
                .setTitleContent("提示", ResultEnum.NOT_SET_PAYWORD.msg, "")
                .setCancelText("")
                .setConfirmText("去设置")
                .setListener({
                    ARouter.getInstance().build(RoutePath.Setting.MODIFY_LOGIN_PASS).withInt("type",1).navigation()
                }, null)
            )
            .show()
    }

    factory(named("not_money")) {
        XPopup.Builder(AppActivityManager.currentActivity())
            .asCustom(CustomInterceptorPopup(AppActivityManager.currentActivity() as ViewBindingBaseActivity<*, *>)
                .setTitleContent("提示", ResultEnum.NOT_MONEY.msg, "")
                .setCancelText("")
                .setConfirmText("去充值")
                .setListener({
                    ARouter.getInstance().build(RoutePath.Wallet.RECHARGE).navigation()
                }, null)
            )
            .show()
    }


}

val allModule = appModule + factoryModule